package com;

import java.io.FileInputStream;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;

public class DBUtil {

	static Connection connection = null;

	public static Connection getConnection() throws SQLException, IOException {
		// if connect is valid, return it
		if (connection != null & !(connection.isClosed())) {
			return connection;
		}
		Properties prop = new Properties();
		FileInputStream in = new FileInputStream(CreateShopConfig.class
				.getClassLoader().getResource("csvimport.properties").getFile());
		prop.load(in);
		String dburl = (String) prop.get("openerpdb");
		String dbuser = (String) prop.get("dbuser");
		String passwd = (String) prop.get("openpgpwd");
		connection = DriverManager.getConnection(dburl, dbuser, passwd);
		return connection;
	}

	public static void closeConnection() throws SQLException {
		if (connection != null && (!connection.isClosed())) {
			connection.close();
		}
	}
}
